//
//  VideoPlayerViewController.m
//  NewMyMusicAPP
//
//  Created by dlios on 15/6/19.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "VideoPlayerViewController.h"


@interface VideoPlayerViewController ()<UMSocialUIDelegate>



@property (retain ,nonatomic) KrVideoPlayerController  *videoController;


@end

@implementation VideoPlayerViewController

-(void)viewWillAppear:(BOOL)animated
{
   FMPlayerVC *fmPlayer = [FMPlayerVC shareHandleController];
    [fmPlayer.audioPlayer pause];

}


- (void)dealloc
{
    
    [_audioPlayer release];
    [_title_ release];
    [_video release];
    [_albumImg release];
    [_url release];
    [_webView release];
    [_segment release];
    [_array release];
    [_playlistUrl release];
    [_videoUrl release];
    [_type release];
    [_Id release];
    [_myImageView release];
    [_related release];
    [_details release];
    [super dealloc];
}





- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    self.view.backgroundColor = [UIColor blackColor];
    
    
    self.navigationController.navigationBar.translucent = NO;//取消navigationController透明度
   
    
    //       给导航栏上放自定义label
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH * 100, HEIGHT * 30)]autorelease];
    [view setBackgroundColor:[UIColor clearColor]];
    self.navigationItem.titleView = view;
    self.title_ = [[[UILabel alloc]initWithFrame:CGRectMake(WIDTH *(-375/2 + 90), 0, 280 * WIDTH, 30 * HEIGHT)]autorelease];
    self.title_.textAlignment = NSTextAlignmentCenter;
    self.title_.textColor = [UIColor whiteColor];
    [self.title_ setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];//加粗字体
    [view addSubview:self.title_];
    
    
    
    
    UIImage *image1 = [UIImage imageNamed:@"VList_Left@2x.png"];
    image1 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    

    //返回
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image1 style:UIBarButtonItemStylePlain target:self action:@selector(stopVideo)];
    
    //分享
    UIImage *image2 = [UIImage imageNamed:@"bottom_icon_share@2x.png"];
    image2 = [image2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image2 style:UIBarButtonItemStylePlain target:self action:@selector(enterInto)];
    
    
    self.details = [[[DetailsView alloc] initWithFrame:CGRectMake(0, HEIGHT * 250, MYWIDTH, HEIGHT * 417) Id:self.Id type:self.type]autorelease];
    self.details.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.details];
    
    
    
    self.related = [[[RelatedView alloc] initWithFrame:CGRectMake(0, HEIGHT * 250, MYWIDTH, HEIGHT * 417) Id:self.Id type:self.type]autorelease];
    self.related.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.related];
    
    
    
    //设置背景图片
    self.myImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, MYWIDTH * 1.3, MYHEIGHT * 1.3)]autorelease];
    self.myImageView.backgroundColor = [UIColor blackColor];
    self.myImageView.image = [UIImage imageNamed:@"IMG_1166.png"];
    [self.view addSubview:self.myImageView];
    [self.view bringSubviewToFront:self.details];
    //图片毛玻璃效果
    UIVisualEffectView *visualEfView = [[[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]]autorelease];
    visualEfView.frame = self.view.frame;
    visualEfView.alpha = 1.0;
    [self.myImageView addSubview:visualEfView];
    
    UIView *myView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, MYWIDTH, MYHEIGHT)]autorelease];
    myView.backgroundColor = [UIColor blackColor];
    myView.alpha = 0.5;
    [self.myImageView addSubview:myView];
    
    
    
    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"MV描述",@"相关MV", nil];
    self.segment = [[[UISegmentedControl alloc] initWithItems:array]autorelease];
    
    self.segment.frame = CGRectMake(WIDTH * 80, HEIGHT * 260, WIDTH * 215, HEIGHT * 35);
    self.segment.backgroundColor = [UIColor clearColor];
    self.segment.tintColor = [UIColor orangeColor];
    
    
    
    [self.segment addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
    
    self.segment.selectedSegmentIndex = 0;//设置默认选择项索引
    self.related.myDelegate = self;
    
    //菊花(小黄人)
    [GiFHUD setGifWithImageName:@"xiaohuangren.gif"];
    [GiFHUD show];
    
    [self asker];
    
  
}
//分享
-(void)enterInto
{
    NSURL *url = [NSURL URLWithString:self.albumImg];
    NSData *data = [[[NSData alloc] initWithContentsOfURL:url]autorelease];
    
    if ([self.videoUrl isEqual:@""]) {
        self.video = [NSString stringWithFormat:@"%@ \n %@",self.navigationItem.title,self.playlistUrl];
    }else
    {
        self.video = [NSString stringWithFormat:@"%@ \n %@",self.navigationItem.title,self.videoUrl];
    }
    
    [self.videoController pause];
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"558b68a867e58ee2a4001769"
                                      shareText:self.video
                                     shareImage:[UIImage imageWithData:data]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,nil]
                                       delegate:self];
}
//点击返回 停止播放MV
-(void)stopVideo
{
    [self.videoController stop];
    
    [GiFHUD dismiss];
    self.videoUrl = nil;
    [self.array removeAllObjects];
    [self playVidel];
    
    [self.navigationController popViewControllerAnimated:YES];
    [self release];
}
 
//重新读取数据  播放视频
-(void)refresh:(NSString *)Id
{
    //菊花(小黄人)
    [GiFHUD setGifWithImageName:@"xiaohuangren.gif"];
    [GiFHUD show];
    
    self.Id = Id;
    self.type = @"VIDEO";
    [self.videoController pause];
    [self asker];
    
    self.details = [[[DetailsView alloc] initWithFrame:CGRectMake(0, HEIGHT * 250, MYWIDTH, HEIGHT * 417) Id:self.Id type:self.type]autorelease];
    self.details.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.details];
    [self.view addSubview:self.myImageView];//图片
    [self.view addSubview:self.related];//相关MV
    [self.view addSubview:self.segment];//按钮
    [self.view bringSubviewToFront:self.videoController.view];//播放视频总在最前
    
}

//网络数据请求
-(void)asker
{
 
    
    NSMutableDictionary *askerDic = [NSMutableDictionary dictionary];
    [askerDic setObject:@"0" forKey:@"D-A"];
    [askerDic setObject:self.Id forKey:@"id"];
    [askerDic setObject:@"true" forKey:@"relatedVideos"];
    [askerDic setObject:@"true" forKey:@"supportHtml"];
    
    
    if ([self.type isEqual:@"VIDEO"]||[self.type isEqualToString:@"PROGRAM"]) {
        
        [Connect ConnectRequestAFWithURL:VIDEODETAIL params:askerDic requestHeader:RequestHeader httpMethod:@"GET" block:^(NSObject *result) {
            
            VideoPlayerModel *videoPlayer = [[[VideoPlayerModel alloc] init]autorelease];
            NSMutableDictionary *dic = (NSMutableDictionary *)result;
            videoPlayer.thumbnailPic = [dic objectForKey:@"thumbnailPic"];
            
            self.title = [dic objectForKey:@"title"];
            self.title_.text = self.title;
            
            if ([dic objectForKey:@"hdUrl"] != 0) {
                self.videoUrl = [dic objectForKey:@"hdUrl"];
              
            }else
            {
                self.videoUrl = [dic objectForKey:@"url"];
              
            }
            
            
            self.albumImg = [dic objectForKey:@"albumImg"];
            
            
            //第三方异步加载图片
            NSURL *url = [NSURL URLWithString:videoPlayer.thumbnailPic];
            // NSLog(@"背景毛玻璃图片 = %@",url);
            [self.myImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:nil]];
            
            [GiFHUD dismiss];//动态图隐藏
            [self.view addSubview:self.segment];
            [self.view bringSubviewToFront:self.videoController.view];
           
            
                [self playVidel];//视频播放
            
            
            
            
            }];
        
    }
    
    if ([self.type isEqual:@"PLAYLIST"]) {
        [Connect ConnectRequestAFWithURL:YueDanDetail params:askerDic requestHeader:RequestHeader httpMethod:@"GET" block:^(NSObject *result) {
            
            VideoPlayerModel *videoPlayer = [[[VideoPlayerModel alloc] init]autorelease];
            NSMutableDictionary *dic = (NSMutableDictionary *)result;
            videoPlayer.thumbnailPic = [dic objectForKey:@"thumbnailPic"];
            
            self.title = [dic objectForKey:@"title"];
            self.title_.text = self.title;//nav上的文字
            
            self.array = [NSMutableArray array];
            NSMutableArray * array = [dic objectForKey:@"videos"];
            
            for (NSMutableDictionary *dic in array) {
                
                if ([dic objectForKey:@"hdUrl"] != 0) {
                    self.playlistUrl = [dic objectForKey:@"hdUrl"];
                }else
                {
                    self.playlistUrl = [dic objectForKey:@"url"];
                }
                
                //self.playlistUrl = [dic objectForKey:@"hdUrl"];
                [self.array addObject:self.playlistUrl];
                
            }
            
            
            //第三方异步加载图片
            NSURL *url = [NSURL URLWithString:videoPlayer.thumbnailPic];
            // NSLog(@"背景毛玻璃图片 = %@",url);
            [self.myImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:nil]];
            
            [GiFHUD dismiss];//动态图隐藏
            [self.view addSubview:self.segment];
            [self.view bringSubviewToFront:self.videoController.view];

           
                [self playVidel];//视频播放
                
            
            
            
        }];
        
    }
    if ([self.type isEqual:@"ACTIVITY"]) {
        //        NSLog(@"WebView");
        //        NSLog(@"url = %@",self.url);
        self.webView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, MYWIDTH, MYHEIGHT - 64)]autorelease];
        NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
        
        [self.webView loadRequest:request];
        [self.view addSubview: self.webView];
        
         [GiFHUD dismiss];//动态图隐藏
    }

    
  //  [GiFHUD dismiss];//动态图隐藏
    
        

    
}



-(void)change:(UISegmentedControl *)segment
{
    [self.view bringSubviewToFront:segment];
    if (segment.selectedSegmentIndex == 0) {
        [self.view addSubview:self.myImageView];
        [self.view bringSubviewToFront:self.details];
    }
    if (segment.selectedSegmentIndex == 1) {
        [self.view addSubview:self.myImageView];
        [self.view bringSubviewToFront:self.related];
    }
    [self.view addSubview:segment];//
    [self.view bringSubviewToFront:self.videoController.view];//播放视频总在最前
}


-(void)playVidel
{
   
    if (self.videoUrl.length != 0) {
        NSURL *url = [NSURL URLWithString:self.videoUrl];
 
        [self addVideoPlayerWithURL:url];
        return;
    }else if ([self.array firstObject] != 0) {
        NSURL *url = [NSURL URLWithString:[self.array firstObject]];
 
        [self addVideoPlayerWithURL:url];
        return;
    }else
    {
        [self addVideoPlayerWithURL:nil];
     
        return;
    }
    
    
}
//返回全屏 显示Bar
-(void)displayBar{
    CATransition *animation = [CATransition animation];
    [animation setDuration:0.5];
    [self.navigationController.view.layer addAnimation:animation forKey:nil];
 
    [self toolbarHidden:NO];
    
}
//点击全屏 隐藏Bar
-(void)hiddenBar{
    CATransition *animation = [CATransition animation];
    [animation setDuration:0.5];
    [self.navigationController.view.layer addAnimation:animation forKey:nil];
   
    [self toolbarHidden:YES];
    
}
-(void)addVideoPlayerWithURL:(NSURL *)url
{

    
    if (!self.videoController) {
    
        self.videoController = [[KrVideoPlayerController alloc] initWithFrame:CGRectMake(0, 0, MYWIDTH, HEIGHT * 250)];
        self.videoController.myDelegate = self;
        __weak typeof(self)weakSelf = self;
        
        
        [self.videoController setDimissCompleteBlock:^{
            weakSelf.videoController = nil;

        }];
        [self.videoController setWillBackOrientationPortrait:^{
            [weakSelf toolbarHidden:NO];

        }];
        [self.videoController setWillChangeToFullscreenMode:^{
            [weakSelf toolbarHidden:YES];
            
        }];
        
        
        [self.view addSubview:self.videoController.view];

        

    }
    self.videoController.contentURL = url;
    
}
//隐藏navigation tabbar 电池栏
-(void)toolbarHidden:(BOOL)Bool{
    
    self.navigationController.navigationBarHidden = Bool;
    [[UIApplication sharedApplication] setStatusBarHidden:Bool withAnimation:UIStatusBarAnimationFade];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
